<?php
defined('IN_TS') or die('Access Denied.');

switch($ts) {

	//预约
	case "order":
		$js = intval($_GET['js']);


		$userid = intval($TS_USER['userid']);

		if ($userid == 0) {
			getJson('请登录后再预约', $js);
		}

		$userData = $new['user']->find('user_info',array(
			'userid'=>$userid
		));
		if($userData['isrenzheng']==0){
			getJson('你还没有取得馆员资格，不能预约服务，请点击<a href="https://shimo.im/docs/fatOK9bmivgLQwuL">这里</a>参加上岗培训取得馆员资格后再预约，注意成绩合格后还要在社区申报证书才能确认馆员资格，如果您已通过考试，请先点击<a href="'.tsUrl('user','apply').'"><strong>这里</strong></a>申报证书。', $js);
		}
		$groupid = trim($_POST['groupid']);
		$dutydate = trim($_POST['dutydate']);
		$beginhour = trim($_POST['beginhour']);
		$beginmin = trim($_POST['beginmin']);
		$endhour = trim($_POST['endhour']);
		$endmin = trim($_POST['endmin']);
		$lunch = trim($_POST['lunch']);
		$memo = trim($_POST['memo']);

		if(!$lunch){
			$lunch = 0;
		}

		$arrGroup = aac('group')->find('group','groupid='.$groupid);

		if($beginhour == ''||$beginmin==''){
			getJson('起始时间必须填写完整', $js);
		}
		if($endhour == ''||$endmin==''){
			getJson('结束时间必须填写完整', $js);
		}
		if ($groupid == '') {
			getJson('服务处所必须填', $js);
		}

		$begintime = strtotime($dutydate.' '.$beginhour.':'.$beginmin);
		$endtime = strtotime($dutydate.' '.$endhour.':'.$endmin);
		if($begintime<time()){
			getJson('开始时间不能早于当前时间', $js);
		}
		if($begintime>$endtime){
			getJson('结束时间不能早于开始时间', $js);
		}
		$str='userid='.$userid.' and cateid=1 and( (begintime>='.$begintime.' and begintime<='.$endtime.') or (endtime>='.$begintime.' and endtime<='.$endtime.') or (begintime<='.$begintime.' and endtime>='.$endtime.'))' ;
		$arrDuty = aac('duty')->find('duty',$str);
		if($arrDuty){
			$group  = aac('group')->find('group',array('groupid'=>$arrDuty['groupid']));
			getJson('预约时间'.$dutydate.' '.$beginhour.':'.$beginmin.'-'.$endhour.':'.$endmin.'与您已经提交的另一条在'.$group['groupname'].'的预约时间（'.date('H:i',$arrDuty['begintime']).'-'.date('H:i',$arrDuty['endtime']).'）冲突', $js);
		}

		if($arrGroup['max']){
			$str = ' and  ((begintime>='.$begintime.' and begintime<='.$endtime.') or (endtime>='.$begintime.' and endtime<='.$endtime.') or (begintime<='.$begintime.' and endtime>='.$endtime.')) ';
			$orderNum = aac('duty')->findCount('duty','groupid ='.$groupid.$str);
			if($orderNum>=$arrGroup['max']){
				$next=intval(($begintime-strtotime('this Monday'))/(7*3600*24))+1;
				getJson('这一时段预约人数过多，请换一个时间或者支援其他馆，点击<a href="'.tsUrl('duty','chart',array('next'=>$next)).'"><strong>这里</strong></a>查看哪个馆缺人。', $js);
			}

		}



		$_SESSION['tsuser']['groupid'] = $groupid;

		$new['user']->create('duty', array(
			'groupid' => $groupid,
			'userid' => $userid,
			'leaderid' => $arrGroup['userid'],
			'begintime' => $begintime,
			'endtime' => $endtime,
			'cateid' => 1,
			'status' => -1,
			'lunch' => $lunch,
			'addtime' => time(),
			'memo' => $memo
		));


		$themonday = strtotime(getAWeekTimeSlot(date('Y-m-d',$begintime)));

		$next = intval(($themonday-strtotime('last Monday'))/(24*3600*7))-1;

		//通知学员
		if($_SESSION['tsuser']['iscoach']==1){
			$strStu = $new['user']->findAll('user_coach',array(
				'coachid'=>$userid,
			));

			foreach($strStu as $k=>$item){
				$strStu=$new['user']->find('user_info',array('userid'=>$item['userid']));
				if($strStu['count_duty']=5){
					//发送消息给学员
					$msg_userid = '0';
					$msg_touserid = $item['userid'];
					$msg_content = '你的教练<a href="'.tsUrl('user','duty',array('id'=>$userid)).'">'.$userData['username'].'</a>刚刚预约了值班，点击后面的链接查看ta预约的值班时间，你可以预约相同时间与ta一起值班。';
					$msg_tourl = tsUrl('duty', 'order', array('groupid'=>$groupid,'next'=>$next));
					aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
				}
			}
		}



		//getJson('服务登记成功。', $js, 1);
		
		// 发送预约成功通知到微信群和同城群
		$url = tsUrl('duty', 'order', array('groupid'=>$groupid,'next'=>$next));

		$username = $TS_USER['username'];
		$groupname = $arrGroup['groupname'];
		$date = '预约时间'.$dutydate.' '.$beginhour.':'.$beginmin.'-'.$endhour.':'.$endmin;
		$msg = "【排班预约】：{$username}刚刚预约了{$groupname}的值班，{$date} ，".
			"大家也可以一起来哦 $url";
		aac('wechat')->sendMsgToGroup($groupid, $msg);
		aac('wechat')->sendMsgToRoom($groupname, $msg, true);
		

		getJson('服务预约成功。', $js, 1, tsUrl('duty', 'order', array('groupid'=>$groupid,'next'=>$next)));


		break;

	//提交服务记录
	case "add":
		$js = intval($_GET['js']);


		$dutyid = trim($_POST['dutyid']);
		if($dutyid==''){
			$userid = trim($_POST['userid']);
		}else{
			$arrDuty = aac("duty")->getOneDuty($dutyid);
			$userid = $arrDuty['userid'];
		}


		if ($userid == '') {
			getJson('请登录后再登记', $js);
		}

		$groupid = trim($_POST['groupid']);
		$begintime = trim($_POST['begindate']);
		$endtime = trim($_POST['enddate']);
		$period = trim($_POST['period']);
		$commute = trim($_POST['commute']);
		$job = delempty(trim($_POST['content']));
		$idcard = trim($_POST['idcard']);
		$username = trim($_POST['username']);
		$cateid = trim($_POST['cateid']);

		if($idcard==0){
			$idcard='';
		}
		//插入图片网址

		$pic = getTextPhotos(tsDecode($job));



		/*$pic  = $_POST['picIds'];

		foreach($pic as $p){
			$image[]="<p><img src='".$p."'></p>";
		}
		$imageurl=implode('',$image);
		*/
		$image = implode(',',$pic);

		//删除正文中的图片和空段落

		$job = strip_tags($job,'<a><br><p>');

		$arrGroup = aac('group')->getOneGroup($groupid);
		$arrUser = aac('user')->getOneUser($userid);

		if ($username == '') {
			getJson('姓名必须填', $js);
		}

		if($dutyid==''&&$cateid==''){
			getJson('请选择服务类型', $js);
		}

		if($begintime==''){
			getJson('起始时间必须填写完整', $js);
		}
		if($endtime==''){
			getJson('结束时间必须填写完整', $js);
		}
		if ($groupid == '') {
			getJson('服务处所必须填', $js);
		}
/*
		if ($job == '') {
			getJson('工作小结及注意事项必须填写', $js);
		}*/

		$begintime = trim(str_replace('AM','',$begintime));
		$begintime = trim(str_replace('PM','',$begintime));
		$begintime = strtotime($begintime);

		$endtime = trim(str_replace('AM','',$endtime));
		$endtime = trim(str_replace('PM','',$endtime));
		$endtime = strtotime($endtime);

		if($begintime>$endtime){
			getJson('结束时间不能早于开始时间', $js);
		}



		if($endtime>time()){
			getJson('不能登记未来的服务时长', $js);
		}

		if($dutyid==''){
			$str='userid='.$userid.' and cateid='.$cateid.' and status>-1 and ( (begintime>='.$begintime.' and begintime<='.$endtime.') or (endtime>='.$begintime.' and endtime<='.$endtime.') or (begintime<='.$begintime.' and endtime>='.$endtime.'))' ;
			$arrDuty = aac('duty')->find('duty',$str);
			if($arrDuty){
					getJson('与已经登记的服务时间冲突', $js);
			}
		}

		$period0 = round(($endtime-$begintime)/3600,1);
		if($period==''||$period>$period0){
			$period = $period0;
		}
		$period =floatval(round($period*2/2,1));



		if($commute==''){
			$commute=0;
		}

		if($period>12){
			getJson('有效服务时间不合理，请检查',$js);
		}
		if($commute>6){
			getJson('通勤时间不合理，请检查',$js);
		}

		$term = floatval(round(($period+$commute*1)*2/2,1));
		//if($idcard!=''){
			//if(strlen($idcard)!=18){
				//getJson('身份证位数错误', $js);
			//}
		//}

		$uparr=array('idcard'=>$idcard,'username'=>$username);
		/*
		if($arrUser['groupids']==''){
			$uparr['groupids'] = $groupid;
		}*/
		if($TS_USER['userid']==$userid||$arrUser['issink']==1){
			$uparr['issink'] = 0;
		}
		$new ['user']->update('user_info',array('userid'=>$userid),$uparr);


		if($dutyid==''){
			$arr=array('groupid' => $groupid,
				'userid' => $userid,
				'leaderid' => $arrGroup['userid'],
				'begintime' => $begintime,
				'day' => date('ymd',$begintime),
				'endtime' => $endtime,
				'commute' => $commute,
				'period' => $period,
				'term' => $term,
				'cateid' => $cateid,
				'job' => $job,
				'addtime' => time(),);
			if($image){
				$arr['image']= $image;
			}


			$dutyid = $new['user']->create('duty', $arr);

			// 发送消息推送到指定的群
			$url = tsUrl('duty','show',array('dutyid'=>$dutyid));
			$duty_cate = aac('duty')->getDutyCate($cateid);
			$duration = aac('duty')->duration($begintime, $endtime);
			$msg = "【服务登记】：{$duration}{$username}在{$arrGroup['groupname']}执行了一次{$duty_cate}，".
				"快去围观吧 $url";
			aac('wechat')->sendMsgToGroup($groupid, $msg);
		}else{
			$arr=array('groupid' => $groupid,
				'leaderid' => $arrGroup['userid'],
				'begintime' => $begintime,
				'endtime' => $endtime,
				'commute' => $commute,
				'period' => $period,
				'term' => $term,
				'cateid' => $cateid,
				'job' => $job,
				'status' => 0,
				'addtime' => time(),);
			if($image){
				$arr['image']= $image;
			}
			$new ['user']->update ( 'duty', array (
				'dutyid' => $dutyid
			), $arr);
		}

		$new ['user']->delete('duty','endtime<'.time().' and status=-1 and userid='. $userid);


		if($userid!= $arrGroup['userid']){
			$arr1 = array('username'=>$arrUser['username'],'term'=>$term);
			foreach($arrGroup['admin'] as $item){
				aac('wechat')->sendwechat(2,$item,$arr1,tsUrl('duty','index',array('groupid'=>$groupid)));
			}
		}

		if($TS_USER['userid']==$userid&&$cateid==1) {

			$strDuty = aac('duty')->findCount('duty', array('userid' => $userid,'cateid'=>1,'status'=>1));
			if($strDuty==0){
				getJson('第一次在分馆值班，有什么感受，可以采访一下你吗？',0,2,'https://jinshuju.net/f/aD3jid');
			}
		}

		header('location:'.tsUrl('duty','show',array('dutyid'=>$dutyid)));
		break;

	//绑文档
	case "bind":
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}
		$dutyid = trim($_POST['dutyid']);
		$articleid = trim($_POST['articleid']);

		if($articleid==''){
			getJson('请选中一篇文档', $js);
		}

		$new['user']->update('article',array(
			'articleid'=>$articleid,
		),array(
			'dutyid'=>$dutyid,
		));

		$new['user']->update('duty',array(
			'dutyid'=>$dutyid,
		),array(
			'articleid'=>$articleid,
		));
		getJson('绑定成功。', 1, 1);

		break;

	//删除服务记录
	case "del":
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}
		$dutyid = trim($_POST['dutyid']);
		$arrDuty = $new['user']->getOneDuty($dutyid);
		if($arrDuty['userid']!=$userid&&$TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$new['user']->delete('duty',array(
			'dutyid'=>$dutyid,
		));

		echo 1;

		break;

	//确认服务记录
	case "verify":
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}

		$dutyid = trim($_POST['dutyid']);

		$arrDuty = aac('duty')->find('duty',array('dutyid'=>$dutyid));
		$arrGroup = aac('group')->getOneGroup($arrDuty['groupid']);
		if(!in_array($userid,$arrGroup['admin'])&&$TS_USER['isadmin']==0){
			getJson('您没有操作权限');
		}
		$new['user']->update('duty',array(
			'dutyid'=>$dutyid,
		),array(
			'status'=>1,
			'addtime'=>time()
		));



		$arrUser = $new['user']->find('user_info',array('userid'=>$arrDuty['userid']));

		$sql = "select sum(term) as sums from ts_duty where userid=".$arrDuty['userid']." and status=1";
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];
		//$arrUser=$new['user']->find('user_info',array('userid'=>$arrDuty['userid']));
		//$count_duty=$arrUser['count_duty']*1+$arrDuty['term']*1;

		$new['user']->update('user_info',array(
			'userid'=>$arrDuty['userid'],
		),array(
			'count_duty'=>$count_duty,'lasttime'=>$arrDuty['begintime'],'isvo'=>1
		));
		//整50祝贺
		$sum_duty = aac('duty')->findCount('duty',array('userid'=>$arrDuty['userid'],'status'=>1));
		if($sum_duty>=50&&$sum_duty%50==0){
			//发广播
			$content = '服务次数达到'.$sum_duty.'，请接受微澜图书馆的致敬！</a>';
			$new['user']->create('weibo',array(
				'userid'=>$arrDuty['userid'],
				'content'=>$content,
				'isaudit'=>0,
				'addtime'=>date('Y-m-d H:i:s'),
				'uptime'=>date('Y-m-d H:i:s'),
			));




		}

		// 对等级进行处理
		$roleid = $new['user']->getRole($count_duty);

		if($roleid > $arrUser['roleid']){

			$new['user']->update('user_info',array(
				'userid'=>$arrDuty['userid'],
			),array(
				'roleid' => $roleid
			));

			$role1 = $new['user']->getRoleName($arrUser['roleid']);
			$role2 = $new['user']->getRoleName($roleid);

			//发一条便笺
			if($roleid==2||$roleid==4||$roleid==7||$roleid==10){
				$content = '<a href="'.tsUrl('user','role').'">服务时长达到'.$count_duty.'，由'.$role1.'升级为'.$role2.'，获得一枚勋章！</a>';
				$new['user']->create('weibo',array(
					'userid'=>$arrDuty['userid'],
					'content'=>$content,
					'isaudit'=>0,
					'addtime'=>date('Y-m-d H:i:s'),
					'uptime'=>date('Y-m-d H:i:s'),
				));
			}

			//微信通知当事人
			if($arrUser['wxopenid']){
				$arr1 = array('role1'=>$role1,'role2'=>$role2);
				aac('wechat')->sendwechat(6,$arrDuty['userid'],$arr1,tsUrl('user','myrole',array('userid'=>$arrDuty['userid'],'roleid'=>$roleid)));
			}

		}



		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$arrDuty['userid']);
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], 'follow',$arrUser['follow_uid']);




		// 对先导的伯乐指数进入处理pp

		if($arrUser['follow_uid']>0){
			$followUser= $new['user']->getOneUser($arrUser['follow_uid']);
			$new['user']->update('user_info',array(
				'userid'=>$arrUser['follow_uid'],
			),array(
				'count_duty_follow'=>$followUser['count_duty_follow']*1+$arrDuty['term']*1,
			));
		}


		// 统计分馆的服务次数并更新数据
		$count_duty1 = $new['user']->findCount ( 'duty', 'groupid='.$arrDuty['groupid'].' and status=1'
		);
		$new['user']->update ( 'group', array (
			'groupid' => $arrDuty['groupid']
		), array (
			'count_duty' => $count_duty1,
		) );

		// 把当事人加入小组
		$arrGroupUser = $new['user']->find('group_user',array('userid'=>$arrDuty['userid'],'groupid'=>$arrDuty['groupid']));

		if(!$arrGroupUser){

			$new['user']->create('group_user',array(
				'userid'=>$arrDuty['userid'],
				'groupid'=>$arrDuty['groupid'],
				'isvo'=>1,
				'addtime'=>time(),
			));

			//更新
			$count_group = $new['user']->findCount('group_user',array(
				'userid'=>$arrDuty['userid'],
			));



			$new['user']->update('user_info',array(
				'userid'=>$arrDuty['userid'],
			),array(
				'count_group'=>$count_group,'groupids'=>getGroupIds($arrDuty['userid'])
			));


			//计算小组会员数
			$count_user = $new['user']->findCount('group_user',array(
				'groupid'=>$arrDuty['groupid'],
			));

			//更新小组成员统计
			$new['user']->update('group',array(
				'groupid'=>$arrDuty['groupid'],
			),array(
				'count_user'=>$count_user,
			));
		}

		$sql = "select sum(term) as sums from ts_duty where userid=".$arrDuty['userid']." and status=1 and groupid=".$arrDuty['groupid'];
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];
		aac('group')->update('group_user',array('groupid'=>$arrDuty['groupid'],'userid'=>$arrDuty['userid']),array('count_duty'=>$count_duty,'lasttime'=>time(),'wechat'=>1));

		//自动获得馆东身份
		$count_duty2 = $new['user']->findCount ( 'duty', 'groupid='.$arrDuty['groupid'].' and status=1 and userid='.$arrDuty['userid'].' and begintime>'.strtotime('-6 month'));
		$arrGroupUser = aac('group')->find('group_user',array('groupid'=>$arrDuty['groupid'],'userid'=>$arrDuty['userid'],'isstock'=>0));
		if($count_duty2>=5&&$arrGroupUser&&$arrGroup['isplace']==2){
			aac('group')->update('group_user',array('groupid'=>$arrDuty['groupid'],'userid'=>$arrDuty['userid'],'isstock'=>0),array('isstock'=>2));
			$new['user']->update('user_info',array('userid'=>$arrDuty['userid']),array('isstock'=>1));
			$content = '<a href="'.tsUrl('group','index').'">半年内累计服务满5次，获得了'.$arrGroup['groupname'].'馆东身份！</a>';
			aac('weibo')->create('weibo',array(
				'userid'=>$arrDuty['userid'],
				'content'=>$content,
				'isaudit'=>0,
				'addtime'=>date('Y-m-d H:i:s'),
				'uptime'=>date('Y-m-d H:i:s'),
			));

			//给当事人发微信通知
			if($arrUser['wxopenid']){
				$arr2 = array(
					'dutyname'=>date('Y-m-d',$arrDuty['begintime']).$arrGroup['groupname'].$arrDuty['cate'],
					'term'=>$arrDuty['term'],
					'count_duty'=>$count_duty
				);
				aac('wechat')->sendwechat(8,$arrDuty['userid'],$arr2,tsUrl('group','newstock',array('groupid'=>$arrDuty['groupid'],'type'=>1)));
			}
		}else{
			//给当事人发微信通知
			if($arrUser['wxopenid']){
				$arr2 = array(
					'dutyname'=>date('Y-m-d',$arrDuty['begintime']).$arrGroup['groupname'].$arrDuty['cate'],
					'term'=>$arrDuty['term'],
					'count_duty'=>$count_duty
				);
				aac('wechat')->sendwechat(4,$arrDuty['userid'],$arr2,tsUrl('duty','card',array('dutyid'=>$arrDuty['dutyid'])));
			}
		}




		getJson('核定成功',1,1);
		break;

	//取消确认
	case "cancel":
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}
		$dutyid = trim($_POST['dutyid']);
		$arrDuty = aac('duty')->find('duty',array('dutyid'=>$dutyid));
		$arrGroup = aac('group')->getOneGroup($arrDuty['groupid']);
		if(!in_array($userid,$arrGroup['admin'])&&$TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$new['user']->update('duty',array(
			'dutyid'=>$dutyid,
		),array(
			'status'=>0,
		));
		$sql = "select sum(term) as sums from ts_duty where userid=".$arrDuty['userid']." and status=1 and groupid=".$arrDuty['groupid'];
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];
		$strDuty = aac('duty')->find('duty',array('groupid'=>$arrDuty['groupid'],'userid'=>$arrDuty['userid'],'status'=>1),null,'addtime desc');
		$lasttime = $strDuty['addtime'];
		aac('group')->update('group_user',array('groupid'=>$arrDuty['groupid'],'userid'=>$arrDuty['userid']),array('count_duty'=>$count_duty,'lasttime'=>$lasttime));


		$sql = "select sum(term) as sums from ts_duty where userid=".$arrDuty['userid']." and status=1";
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];

		$roleid = $new['user']->getRole($count_duty);

		$new['user']->update('user_info',array(
			'userid'=>$arrDuty['userid'],
		),array(
			'count_duty'=>$count_duty,
			'roleid' => $roleid
		));

		// 对积分进行处理
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], $TS_URL['ts'],$arrDuty['userid']);
		aac('user') -> doScore($TS_URL['app'], $TS_URL['ac'], 'unfollow',$arrUser['follow_uid']);

		// 对先导的伯乐指数进入处理

		if($arrUser['follow_uid']>0){
			$followUser= $new['user']->getOneUser($arrUser['follow_uid']);
			$new['user']->update('user_info',array(
				'userid'=>$arrUser['follow_uid'],
			),array(
				'count_duty_follow'=>$followUser['count_duty_follow']*1-$arrDuty['term']*1,
			));

		}

		// 统计分馆的服务次数并更新数据
		$count_duty = $new['user']->findCount ( 'duty', 'groupid='.$arrDuty['groupid'].' and status=1'
		);

		$new['user']->update ( 'group', array (
			'groupid' => $arrDuty['groupid']
		), array (
			'count_duty' => $count_duty,
		) );

		echo 1;

		break;
	case 'star':
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}

		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$dutyid = trim($_POST['dutyid']);
		$new['user']->update('duty',array(
			'dutyid'=>$dutyid,
		),array(
			'star'=>1,
		));
		echo 1;
		break;
	case 'nostar':
		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);
		if ($userid == 0) {
			getJson('请登录后再操作', $js);
		}

		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$dutyid = trim($_POST['dutyid']);
		$new['user']->update('duty',array(
			'dutyid'=>$dutyid,
		),array(
			'star'=>0,
		));
		echo 1;
		break;

	case "renzheng":
		$js = intval($_GET['js']);

		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'isvo'=>1,
			'isrenzheng' => 1,
			'renzhengtime'=>time()
		) );

		echo 1;
		break;


	case "cancelrenzheng":
		$js = intval($_GET['js']);

		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'isrenzheng' => 0,
			'renzhengtime'=>0
		) );

		echo 1;
		break;

	case "office":
		$js = intval($_GET['js']);

		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'deoffice' => 0,
		) );

		echo 1;
		break;


	case "deoffice":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'deoffice' => 1,
		) );

		echo 1;
		break;

	case "sink":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		//if($TS_USER['isadmin']==0){
			//getJson('您没有操作权限', $js);
		//}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'issink' => 1,
		) );

		echo 1;
		break;

	case "desink":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		//if($TS_USER['isadmin']==0){
			//getJson('您没有操作权限', $js);
		//}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'issink' => 0,
		) );

		echo 1;
		break;

	case "to":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'isto' => 1,'isvo'=>1,'isrenzheng'=>1
		) );

		echo 1;
		break;

	case "deto":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'isto' => 0,
		) );
		echo 1;
		break;

	case "tui":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'istui' => 1,'isvo'=>1,'isrenzheng'=>1
		) );

		echo 1;
		break;

	case "detui":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'istui' => 0,
		) );
		echo 1;
		break;

	case "resetfollow":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}

		$new['user']->update ( 'user_info', array (
			'userid' => $TS_USER['userid']
		), array (
			'follow_uid' => 0,
		) );

		echo 1;
		break;

	case "setorg":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$orgid = trim($_POST['orgid']);
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'orgid' => $orgid,
		) );
		echo 1;
		break;

	case "setgroup":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$groupids = trim($_POST['groupid']);
		$strUser = aac('user')->getOneUser($userid);

		$arrGroup = explode(',',$groupids);
		$arrGroup0 = $strUser['groupids'];
		foreach($arrGroup0 as $item) {
			aac('group')->quitgroup($userid,$item);
		}

		foreach($arrGroup as $groupid){
			aac('group')->joingroup($userid,$groupid);

			$new['user']->update ( 'user_apply', array (
				'userid' => $userid
			), array (
				'groupid' => $groupid,
			) );
		}

		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'groupids' => $groupids,
		) );

		echo 1;
		break;

	case "setname":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$username = trim($_POST['username']);
		if ($username ==''){
			getJson('姓名不能为空', $js);
		}
		$new['user']->update ( 'user_info', array (
			'userid' => $userid
		), array (
			'username' => $username,
		) );
		echo 1;
		break;

	case "postcomment":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$userid = trim($_POST['userid']);
		$groupid = trim($_POST['groupid']);
		$type = trim($_POST['type']);
		$content = trim($_POST['content']);
		$text = strip_tags($_POST['content'],'<img>');
		if($text == ''){
			tsNotice('评论内容不能为空');
		}

		if($groupid){
		aac('group')->update('group_user',array('groupid'=>$groupid,'userid'=>$userid),array('supporter'=>$TS_USER['userid'],'uptime'=>time()));
		}

		$arr = array (
			'userid' => $userid,
			'authorid' => $TS_USER['userid'],
			'content' => $content,
			'addtime' => time(),
			'uptime' =>time()

		);

		if($groupid){
			$arr['type']=1;
		}

		$new['user']->create ( 'user_comment',$arr);

		if($groupid){
			if($type){
				header('location: '.tsUrl('group','vo'));
			}else{
				header('location: '.tsUrl('group','stock'));
			}

		}else{
			header('location: '.$_SERVER['HTTP_REFERER']);
		}


		break;

	case "delcomment":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$commentid = trim($_POST['commentid']);


		$new['user']->delete ( 'user_comment', array(
			'commentid' => $commentid
		));
		echo 1;
		break;

	case "dogroup":
		$arrNewUserId = $new['user']->findAll('user_info','groupid=0','addtime desc','userid','');
		foreach($arrNewUserId as $item){

				$duty = aac('duty')->find('duty','userid='.$item['userid'],'groupid','addtime desc');

				if($duty){

					$new['user']->update ( 'user_info', array (
						'userid' => $item['userid']
					), array (
						'groupid' => $duty['groupid'],
					) );
				}

		}
	    echo 1;
		break;

	case "apply":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}

		$arrType = $_POST['type'];
		$groupid = $_POST['groupid'];

		if($groupid){
			$group = aac('group')->find('group',array('groupid'=>$groupid));
		}

		if(!$arrType){
			getJson('请选择证章种类！', $js);
		}
		$score = trim($_POST['score']);
		foreach($arrType as $type){
			if($type){
				if($type==1) {
					if (!$score) {
						getJson('请填写自测分数！', $js);
					} else {
						if ($score < 80) {
							getJson('成绩不及格不能申请证书！', $js);
						} else {
							$new['user']->update('user_info', array('userid' => $TS_USER['userid']), array('isvo' => 1, 'isrenzheng' => 1));
							$_SESSION['tsuser']['isvo']	= 1;
							$_SESSION['tsuser']['isrenzheng']	= 1;
							$new['user']->update('group_user', array('userid' => $TS_USER['userid']), array('wachat' => 1));
							//发一条动态
							$content = '通过了馆员资格考试，取得了馆员资格！';
							aac('weibo')->create('weibo',array(
								'userid'=>$TS_USER['userid'],
								'content'=>$content,
								'isaudit'=>0,
								'addtime'=>date('Y-m-d H:i:s'),
								'uptime'=>date('Y-m-d H:i:s'),
							));

							//发送消息给本人
							$msg_userid = '0';
							$msg_touserid = $TS_USER['userid'];
							$msg_content = '你已取得馆员资格，请向小新（微信号xingongmin2007）发送口令“'.$group['groupname'].'馆员”以加入馆员群；你的馆员证书已经生成，请点击链接查看，可长按证书下方按钮下载图片。';
							$msg_tourl = tsUrl('group','cert1');
							aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);


						}
					}
				}



				$user =  $new['user']->find('user_info',array('userid'=>$TS_USER['userid']));
				if($type==2&&$user['roleid']<2||$type==3&&$user['roleid']<4||$type==4&&$user['roleid']<7||$type==5&&$user['roleid']<10){
					getJson('您申请的勋章高于您当前的等级，请继续努力！', $js);
				}

				$arrApply = $new['user']->find('user_apply',array('userid'=>$TS_USER['userid'],'type'=>$type));
				if($arrApply){
					getJson('请勿重复申请！', $js);
				}

				$arrgroupid = explode(',',$user['groupids']);
				$groupid = end($arrgroupid);

				$arr = array(
					'userid'=>$TS_USER['userid'],
					'type' => $type,
					'groupid' => $groupid,
					'addtime'=> time()
				);
				if($type==1){
					$arr['score']=$score;
				}
				$new['user']->create('user_apply',$arr);

				if($type==1){
					$url = tsUrl('user','apply',array('sort'=>1,'groupid'=>$groupid));


					//发送消息给学员
					$msg_userid = '0';
					$msg_touserid = $TS_USER['userid'];
					$msg_content = '你已经成为候任馆员，请尽量在两周内值班一次，以升级为试用馆员。你可以点击后面的链接查看教练名单，如果教练名单不为空，你可以选择一个教练，跟随ta。';
					$msg_tourl = tsUrl('group', 'user', array('id'=>$groupid,'usertype'=>4));
					aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);



				}else{
					$url = tsUrl('user','apply');
				}
			}

		}

		header('location: '.$url);
		break;

	case "issue":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$applyid = trim($_POST['applyid']);
		$new['user']->update ( 'user_apply', array (
			'applyid' => $applyid
		), array (
			'status' => 1,
		) );
		echo 1;
		break;

	case "deissue":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$applyid = trim($_POST['applyid']);
		$new['user']->update ( 'user_apply', array (
			'applyid' => $applyid
		), array (
			'status' => 0,
		) );
		echo 1;
		break;


	case "refuse":
		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}
		$applyid = trim($_POST['applyid']);
		$new['user']->update ( 'user_apply', array (
			'applyid' => $applyid
		), array (
			'status' => 2,
		) );
		echo 1;
		break;

	case "upload_excel":
		if($TS_USER ['isadmin'] == 0) tsNotice('非管理员不允许导入数据！');
		$arrUpload = tsUpload($_FILES['excelData'],1,'user',array('xls','xlsx'));
		$data= importExecl('uploadfile/user/'.$arrUpload['url']);
		foreach ($data as $k => $v) {

			if ($k != 0) {
				$arrGroup = aac('group')->find('group', array(
					'groupname' => $v['A'],
				));
				$groupid=$arrGroup['groupid'];
				$arrUser =$new['user']->find('user_info', array(
					'username' => $v['B'],'groupid' => $groupid
				));
				$arr=array(
					'groupid' => $groupid,
					'userid' => $arrUser['userid'],
					'type' => $v['C'],
					'score' => $v['D'],
					'addtime' => intval(($v['E'] - 25569) * 3600 * 24),
					);

				$new['user']->create('user_apply', $arr);


			}

		}
		getJson('导入成功。', $js, 2, tsUrl('user','apply'));
		break;

	case "setlast":
		$arrUser = $new['user']->findAll('user_info','groupid>0');
		foreach ($arrUser as $v){
			$duty = aac('duty')->find('duty',array('userid'=>$v['userid'],'status'=>1),'addtime','addtime desc');
			$new['user']->update('user_info',array('userid'=>$v['userid']),array('lasttime'=>$duty['addtime']));
		}
		getJson('操作成功。', $js, 1);
		break;

	case 'isee':
		$noticeid = trim($_POST['noticeid']);
		$type = trim($_POST['type']);
		if(!isset($_COOKIE['read_notice'])){
			$readid = $noticeid;
		}else{
			$readid = $readid.','.$noticeid;
		}
		setcookie('read_notice',$readid,time()+3600*72);
		getJson('操作成功。', $js, 1);
		break;

	case 'export':
		$beginyear = trim($_POST['beginyear']);
		$beginmonth = trim($_POST['beginmonth']);
		$beginday = trim($_POST['beginday']);
		$endyear = trim($_POST['endyear']);
		$endmonth = trim($_POST['endmonth']);
		$endday = trim($_POST['endday']);
		$type = trim($_POST['type']);
		$provincce = trim($_POST['province']);
		$city = trim($_POST['city']);

		$str='';

		if($beginyear!=''&&$beginmonth!=''&&$beginday!=''&&$endyear!=''&&$endmonth!=''&&$endday!=''){
			$begindate = $beginyear . "-" . $beginmonth . "-" . $beginday;
			$enddate = $endyear . "-" . $endmonth . "-" . $endday;
			$str =$str.' and '.$type.'>'.strtotime($begindate).' and '.$type.'<'.strtotime($enddate);
		}
		if($provincce!=''){
			$str = $str.' and province='.$provincce;
		}
		if($city!=''){
			$str = $str.' and city='.$city;
		}

		//Excel表格式,这里简略写了8列
		$letter = array('A','B','C','D','E','F','G');
		//表头数组
		$tableheader = array('姓名','性别','身份证号','手机号','所属团队','常住地','服务时长');
		//$arrDuty = $this->findAll('duty',$str,null,null,null,null);
		$arrUser=$new['user']->findAll('user_info','idcard<>"" and idcard<>0 and count_duty>0'.$str,'userid desc');
		$data=array();
		foreach ( $arrUser as $key => $item ){
			$strGroup = explode(',',$item['groupids']);
			$group= aac('group')->find('group',array('groupid'=>$strGroup[0]));
			$data[$key]=array($item['username'],$item['sex'],$item['idcard']."\t",$item['phone'],$group['groupname'],$item['province'].$item['city'],$item['count_duty']);
		}
		$filename='成员名单.xls';
		create_xls($data,$letter,$tableheader,$filename);

		output_xls($data,$filename);
		getJson('操作成功',1,1);

		break;

	//设为月捐人
	case "setstock":

		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}

		$userid= intval($_POST['userid']);

		$new['user']->update('user_info',array('userid'=>$userid),array('isstock'=>1));
		aac('group')->update('group_user',array('userid'=>$userid),array('isstock'=>1));

		echo '1';exit;
		break;


	//设为月捐人
	case "destock":

		$js = intval($_GET['js']);
		if (intval($TS_USER['userid']) == 0) {
			getJson('请登录后再操作', $js);
		}
		if($TS_USER['isadmin']==0){
			getJson('您没有操作权限', $js);
		}

		$userid= intval($_POST['userid']);

		$new['user']->update('user_info',array('userid'=>$userid),array('isstock'=>-2));
		aac('group')->update('group_user',array('userid'=>$userid),array('isstock'=>-2));

		echo '1';exit;
		break;

	case 'dosetstock':
		$strUser = $new['user'] ->findAll('user_info',array('isstock'=>0));
		foreach($strUser as $item){
			$strGroupUser = aac('group')->findAll('group_user',array('userid'=>$item['userid'],'isstock'=>1));
			if($strGroupUser){
				$new['user'] ->update('user_info',array('userid'=>$item['userid']),array('isstock'=>1));
			}
		}
		break;

	case 'dousercomm':
		$strComm = $new['user'] ->findAll('user_comment');
		foreach($strComm as $item){
			$strUser = $new['user'] ->find('group_user','userid='.$item['userid'].' and supporter>0');
			if($strUser){
				$new['user']->update('user_comment',array('commentid'=>$item['commentid']),array('type'=>1));
			}
		}

		break;

	case 'setcoach':
		$js = intval($_GET['js']);
		$type= intval($_POST['type']);
		$new['user'] ->update('user_info',array('userid'=>$TS_USER['userid']),array('iscoach'=>$type));
		$_SESSION['tsuser']['iscoach']=$type;
		getJson('设为教练成功！',$js,1);
		break;

	case 'docoach':
		$js = intval($_GET['js']);
		$type= intval($_POST['type']);
		$userid= intval($_POST['userid']);
		if($type==1){
			$strCoach = $new['user']->find('user_coach',array('userid'=>$TS_USER['userid'],'coachid'=>$userid));
			if(!$strCoach){
				$new['user']->create('user_coach',array('userid'=>$TS_USER['userid'],'coachid'=>$userid,'addtime'=>time()));

				//发送消息给教练
				$msg_userid = '0';
				$msg_touserid = $userid;
				$msg_content = '候任馆员'.$TS_USER['username'].'把你设置为教练了，你可以到群里找到ta，加为微信好友，私信解答ta的困惑，鼓励ta预约值班，争取早日转正。';
				$msg_tourl = tsUrl('user', 'duty', array('id'=>$TS_USER['userid']));
				aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
			}
		}
		if($type==0){
				$new['user']->delete('user_coach',array('userid'=>$TS_USER['userid'],'coachid'=>$userid));
		}
		if($type==-1){
			$new['user']->update('user_coach',array('userid'=>$TS_USER['userid'],'coachid'=>$userid),array('status'=>-1));
		}

		getJson('操作成功！',$js,1);
		break;
	case 'setleadership':
		$strUser = aac('user')->findAll('user_info','leadership is not null');
		foreach($strUser as $v){
			aac('group')->update('group_user',array('userid'=>$v['userid']),array('leadership'=>$v['leadership']));
		}
		break;
	case 'update_groupids':
		$arrUser = aac('user')->findAll('user_info','groupids is not null');
		foreach($arrUser as $v){
			$groupids = getGroupIds($v['userid']);
			if($groupids){
				aac('user')->update('user_info',array('userid'=>$v['userid']),array('groupids'=>$groupids));
			}else{
				aac('user')->update('user_info',array('userid'=>$v['userid']),array('groupids'=>null));
			}

		}
	    break;
	case 'reuptime':
		$strUser = aac('user')->findAll('user_info','groupids is not null');
		foreach($strUser as $v){
			aac('group')->update('group_user',array('userid'=>$v['userid']),array('uptime'=>$v['uptime']));
		}
		break;
	case 'setupline':
		$strUser = aac('user')->findAll('user_follow');
		foreach($strUser as $v){
			aac('user')->update('user_info',array('userid'=>$v['userid']),array('upline'=>$v['userid_follow']));
		}
	    break;
}




